import axios, {type AxiosInstance} from 'axios'
import router from '@/router'

export class BackendService {
    private client: AxiosInstance | undefined

    constructor(baseURL: string) {
        this.client = axios.create({
            baseURL: baseURL
        })

        this.client.interceptors.request.use(
            (config) => {
                return config
            },
            (error: any) => {
                return Promise.reject(error)
            }
        )

        this.client.interceptors.response.use(
            function (response) {
                return response
            },
            function (error) {
                const response = error.response
                if (response) {
                    if (response.status === 401) {
                        router.push({ path: '/login' }).finally()
                        return Promise.reject(error)
                    }
                }
                return Promise.reject(error)
            }
        )
    }

    public async post(url: string, data: any): Promise<any> {
        return this.client?.post(url, data)
    }
}

export const backendService: BackendService = new BackendService(
    import.meta.env.VITE_BACKEND_API_URL
)


export enum ResponseCode {
    /**
     * 成功
     */
    OK = 'OK',

    /**
     * 数据校验不通过，无效参数
     */
    METHOD_ARGUMENT_NOT_VALID = 'METHOD_ARGUMENT_NOT_VALID',

    /**
     * 服务器内部错误
     */
    INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR'
}
